package com.ruicar.afs.cloud.basic.common.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.basic.common.condition.RevokeHandleCondition;
import com.ruicar.afs.cloud.basic.common.entity.BasicMainInfo;
import com.ruicar.afs.cloud.basic.common.entity.BasicRevokeHandle;
import com.ruicar.afs.cloud.basic.common.service.BasicMainInfoService;
import com.ruicar.afs.cloud.basic.common.service.BasicReceiptRecordService;
import com.ruicar.afs.cloud.basic.common.service.BasicRevokeHandleService;
import com.ruicar.afs.cloud.common.core.enums.AfsEnumUtil;
import com.ruicar.afs.cloud.common.core.security.util.SecurityUtils;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.common.modules.contract.enums.ProcessStatusEnum;
import com.ruicar.afs.cloud.common.modules.contract.enums.TreatmentMethodEnum;
import com.ruicar.afs.cloud.common.util.EmptyUtils;
import com.ruicar.afs.cloud.common.vo.RevokeHandleVo;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.text.MessageFormat;
import java.util.Calendar;

/**
 * @author infinity
 * @version 1.0
 * @date 2020/9/3 13:39
 * @description: 微信和代扣撤销成功后续处理
 */
@Slf4j
@RestController
@RequestMapping("/revokeHandle")
@AllArgsConstructor
public class BasicRevokeHandleController {
    private final BasicRevokeHandleService basicRevokeHandleService;
    private final BasicMainInfoService basicMainInfoService;
    private final BasicReceiptRecordService basicReceiptRecordService;

    @ApiOperation(value = "根据条件查询收款撤销成功数据")
    @PostMapping("/getRevokeHandleList")
    public IResponse<RevokeHandleVo> getRevokeHandleList(@RequestBody RevokeHandleCondition condition) {
        String[] processStatus={AfsEnumUtil.key(ProcessStatusEnum.draft),AfsEnumUtil.key(ProcessStatusEnum.refuse)};
        condition.setProcessStatus(processStatus);
        return basicRevokeHandleService.getRevokeHandleList(condition);
    }

    @ApiOperation(value = "根据条件查询收款撤销成功数据")
    @PostMapping("/getRevokeHandleVo")
    public IResponse getRevokeHandleVo(@RequestParam("receiptId") Long receiptId) {
        BasicRevokeHandle basicRevokeHandle = basicRevokeHandleService.getOne(Wrappers.<BasicRevokeHandle>lambdaQuery().eq(BasicRevokeHandle::getReceiptId, receiptId), false);
        if (EmptyUtils.isNotEmpty(basicRevokeHandle)) {
            RevokeHandleVo vo = new RevokeHandleVo();
            BeanUtil.copyProperties(basicRevokeHandle, vo);
            BasicMainInfo basicMainInfo = basicMainInfoService.getOne(Wrappers.<BasicMainInfo>lambdaQuery().eq(BasicMainInfo::getContractNo, vo.getContractNo()));
            Assert.isTrue(basicMainInfo != null, MessageFormat.format("合同编号：{0}，合同信息不存在！", vo.getContractNo()));
            vo.setApplyNo(basicMainInfo.getApplyNo());
            return IResponse.success(vo);
        } else {
            return IResponse.fail("合同系统中无此撤销id");
        }
    }

    /**
     * 撤销处理发起撤销信息处理
     *
     * @param
     * @return
     */
    @PostMapping(value = "/revokeHandleStart")
    @ApiOperation(value = "撤销处理发起撤销信息处理")
    @Transactional(rollbackFor = Exception.class)
    public IResponse revokeHandleStart(@RequestParam("receiptId") Long receiptId, @RequestParam("treatmentMethod") String treatmentMethod,@RequestParam("caseNo") String caseNo) {
        BasicRevokeHandle basicRevokeHandle = basicRevokeHandleService.getOne(Wrappers.<BasicRevokeHandle>lambdaQuery().eq(BasicRevokeHandle::getReceiptId, receiptId), false);
        if (EmptyUtils.isNotEmpty(basicRevokeHandle)) {
            Assert.isTrue((ProcessStatusEnum.draft.equals(basicRevokeHandle.getProcessStatus())
                    || ProcessStatusEnum.refuse.equals(basicRevokeHandle.getProcessStatus())), "该数据已在流程中！");
            basicRevokeHandle.setProcessStatus(ProcessStatusEnum.underReview);
            basicRevokeHandle.setStartTime(Calendar.getInstance().getTime());
            basicRevokeHandle.setEndTime(null);
            basicRevokeHandle.setTreatmentMethod((TreatmentMethodEnum) AfsEnumUtil
                    .getEnum(treatmentMethod, TreatmentMethodEnum.class));
            basicRevokeHandle.setCaseNo(caseNo);
            basicRevokeHandleService.updateById(basicRevokeHandle);
            return IResponse.success("处理成功");
        } else {
            return IResponse.fail("合同系统中无此撤销数据");
        }
    }

    /**
     * 撤销处理审批通过撤销信息处理
     *
     * @param
     * @return
     */
    @PostMapping(value = "/revokeHandleAgree")
    @ApiOperation(value = "撤销处理通过撤销信息处理")
    @Transactional(rollbackFor = Exception.class)
    public IResponse revokeHandleAgree(@RequestParam("receiptId") Long receiptId) {
        BasicRevokeHandle basicRevokeHandle = basicRevokeHandleService.getOne(Wrappers.<BasicRevokeHandle>lambdaQuery().eq(BasicRevokeHandle::getReceiptId, receiptId), false);
        if (EmptyUtils.isNotEmpty(basicRevokeHandle)) {
            Assert.isTrue((!ProcessStatusEnum.draft.equals(basicRevokeHandle.getProcessStatus())
                    && !ProcessStatusEnum.refuse.equals(basicRevokeHandle.getProcessStatus())), "该数据已在流程中！");
            basicRevokeHandle.setProcessStatus(ProcessStatusEnum.approved);
            basicRevokeHandle.setEndTime(Calendar.getInstance().getTime());
            basicRevokeHandleService.updateById(basicRevokeHandle);
            if (TreatmentMethodEnum.REFUND.equals(basicRevokeHandle.getTreatmentMethod())) {
                basicReceiptRecordService.handleReceiptForRefund(basicRevokeHandle.getReceiptId(), SecurityUtils.getUser().getUserRealName());
                basicRevokeHandleService.handleReturn(basicRevokeHandle);
            } else {
                basicReceiptRecordService.handleIncome(receiptId, basicRevokeHandle.getSourceType());
            }
            return IResponse.success("处理成功");
        } else {
            return IResponse.fail("合同系统中无此撤销id");
        }


    }

    /**
     * 撤销处理审批拒绝撤销信息处理
     *
     * @param
     * @return
     */
    @PostMapping(value = "/revokeHandleReject")
    @ApiOperation(value = "撤销处理拒绝撤销信息处理")
    @Transactional(rollbackFor = Exception.class)
    public IResponse revokeHandleReject(@RequestParam("receiptId") Long receiptId) {
        BasicRevokeHandle basicRevokeHandle = basicRevokeHandleService.getOne(Wrappers.<BasicRevokeHandle>lambdaQuery().eq(BasicRevokeHandle::getReceiptId, receiptId), false);
        if (EmptyUtils.isNotEmpty(basicRevokeHandle)) {
            basicRevokeHandle.setProcessStatus(ProcessStatusEnum.refuse);
            basicRevokeHandle.setEndTime(Calendar.getInstance().getTime());
            basicRevokeHandleService.updateById(basicRevokeHandle);
            return IResponse.success("处理成功");
        } else {
            return IResponse.fail("合同系统中无此撤销id");
        }
    }
}
